/*
 * Copyright (c) 2018, ZeeSec Technologies. All rights reserved.
 * ZeeSec PROPRIETARY/CONFIDENTIAL.
 */

package springtest.quartz;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author ZhiQiang Chen
 * @date 2018-08-29 14:44:33
 */

public class QuartzManager {
    private static SchedulerFactory schedulerFactory = new StdSchedulerFactory();

    private static final Logger log = LoggerFactory.getLogger(QuartzManager.class);

    // 添加新的jop
    public static void addJop(String jobName, Class<? extends Job> cls, String cronExpression, String jobGroup)
            throws SchedulerException {
        // 初始化scheduler
        Scheduler scheduler = schedulerFactory.getScheduler();
        // 创建job
        JobDetail detail = JobBuilder.newJob(cls).withIdentity(jobName, jobGroup).build();
        // 创建触发器 trigger
        // TriggerBuilder.newTrigger().withSchedule(CronScheduleBuilder.cronSchedule("")).build();
        Trigger trigger = TriggerBuilder.newTrigger().build();
        // 交给quar执行任务
        scheduler.scheduleJob(detail, trigger);
        // schdeuler 是否启动
        if (scheduler.isShutdown()) {
            scheduler.start();
        }
    }

    // 修改任务的触发事件
    public static void modifyJobTime(String jobName, String newCronExpression, String jobGroup)
            throws SchedulerException {
        Scheduler scheduler = schedulerFactory.getScheduler();
        CronTrigger trigger = (CronTrigger) scheduler.getTrigger(new TriggerKey(jobName, jobGroup));
        if (trigger == null)
            return;
        String cronExpression = trigger.getCronExpression();

    }

    // 删除一个任务
    public static void removeJop(String jobName, String jobGroupName) {
        try {
            Scheduler scheduler = schedulerFactory.getScheduler();
            TriggerKey triggerKey = new TriggerKey(jobName, jobGroupName);
            // 停止触发器
            scheduler.pauseTrigger(triggerKey);
            // 移除触发器
            scheduler.unscheduleJob(triggerKey);
            // 删除任务
            scheduler.deleteJob(JobKey.jobKey(jobName, jobGroupName));
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    // 启动所有定时任务
    public static void startJobs() {
        try {
            Scheduler sched = schedulerFactory.getScheduler();
            if (sched.isShutdown())
                sched.start();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    // 关闭所有定时任务
    public static void shutdownJops() {
        try {
            Scheduler sched = schedulerFactory.getScheduler();
            if (!sched.isShutdown()) {
                sched.shutdown();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void getTrigger() {
        Trigger trigger = TriggerBuilder.newTrigger()
                .withSchedule(CronScheduleBuilder.monthlyOnDayAndHourAndMinute(0, 0, 0)).build();
    }

}
